package com.example.project.dao.sqls;

import com.example.project.entity.TableB;
import org.apache.ibatis.jdbc.SQL;

/**
 * Created by Ethan on 2022-08-25 18:08
 */
public class TableBProvider {
    public String findAllSql() {
        return new SQL()
                .SELECT("*")
                .FROM("b")
                .toString();
    }

    public String insertSql() {
        return new SQL()
                .INSERT_INTO("b")
                .VALUES("id,A,B,C,D,E,F,aa,correct_aa,bb,correct_bb,cc,correct_cc,dd,correct_dd,ee,correct_ee",
                        "#{id},#{a},#{b},#{c},#{d},#{e},#{f},#{aa},#{correctAa},#{bb},#{correctBb},#{cc},#{correctCc},#{dd},#{correctDd},#{ee},#{correctEe}")
                .toString();
    }

    public String deleteSql() {
        return new SQL()
                .DELETE_FROM("b")
                .WHERE("C=#{c}")
                .toString();
    }

    public String updateSql(final TableB tableB) {
        return new SQL() {{
            UPDATE("b");
            if (tableB.getA() != null) {
                SET("A=#{a}");
            }
            if (tableB.getB() != null) {
                SET("B=#{b}");
            }
            if (tableB.getC() != null) {
                SET("C=#{c}");
            }
            if (tableB.getD() != null) {
                SET("D=#{d}");
            }
            if (tableB.getE() != null) {
                SET("E=#{e}");
            }
            if (tableB.getF() != null) {
                SET("F=#{f}");
            }
            if (tableB.getAa() != null) {
                SET("aa=#{aa}");
            }
            if (tableB.getCorrectAa() != null) {
                SET("correct_aa=#{correctAa}");
            }
            if (tableB.getBb() != null) {
                SET("bb=#{bb}");
            }
            if (tableB.getCorrectBb() != null) {
                SET("correct_bb=#{correctBb}");
            }
            if (tableB.getCc() != null) {
                SET("cc=#{cc}");
            }
            if (tableB.getCorrectCc() != null) {
                SET("correct_cc=#{correctCc}");
            }
            if (tableB.getDd() != null) {
                SET("dd=#{dd}");
            }
            if (tableB.getCorrectDd() != null) {
                SET("correct_dd=#{correctDd}");
            }
            if (tableB.getEe() != null) {
                SET("ee=#{ee}");
            }
            if (tableB.getCorrectEe() != null) {
                SET("correct_ee=#{correctEe}");
            }
            WHERE("id=#{id}");
        }}.toString();
    }

    public String getSumAaSql() {
        return new SQL()
                .SELECT("sum(aa)")
                .FROM("b")
                .toString();
    }

    public String getSumCoAaSql() {
        return new SQL()
                .SELECT("sum(correct_aa)")
                .FROM("b")
                .toString();
    }

    public String getSumBbSql() {
        return new SQL()
                .SELECT("sum(bb)")
                .FROM("b")
                .toString();
    }

    public String getSumCoBbSql() {
        return new SQL()
                .SELECT("sum(correct_bb)")
                .FROM("b")
                .toString();
    }

    public String getSumCcSql() {
        return new SQL()
                .SELECT("sum(cc)")
                .FROM("b")
                .toString();
    }

    public String getSumCoCcSql() {
        return new SQL()
                .SELECT("sum(correct_cc)")
                .FROM("b")
                .toString();
    }

    public String getSumDdSql() {
        return new SQL()
                .SELECT("sum(dd)")
                .FROM("b")
                .toString();
    }

    public String getSumCoDdSql() {
        return new SQL()
                .SELECT("sum(correct_dd)")
                .FROM("b")
                .toString();
    }

    public String getSumEeSql() {
        return new SQL()
                .SELECT("sum(ee)")
                .FROM("b")
                .toString();
    }

    public String getSumCoEeSql() {
        return new SQL()
                .SELECT("sum(correct_ee)")
                .FROM("b")
                .toString();
    }
}
